package com.socool.sinofaith.ui;

import com.socool.sinofaith.R;
import com.socool.sinofaith.SocoolApplication;
import com.socool.sinofaith.model.AppUserInfo;
import com.socool.sinofaith.util.S;
import com.socool.sinofaith.util.S.State;
import com.socool.sinofaith.util.SinofaithUtil;
import com.socool.sinofaith.view.MyDialog;
import com.socool.sinofaith.view.TiPsDialog;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class SettingFragment extends Fragment implements OnClickListener {

	private LinearLayout ll_modifypass, ll_about, ll_notice, ll_update, ll_delete, ll_user, ll_investigate;
	private Button logOut;
	private TiPsDialog mTiPsDialog;

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mTiPsDialog = new TiPsDialog(this.getActivity(), R.style.dialog, null, "离线登录状态下该功能不可用");
		View view = inflater.inflate(R.layout.fragment_setting, null);
		SocoolApplication.addActivity(this.getActivity());
		ll_modifypass = (LinearLayout) view.findViewById(R.id.ll_modifypass);
		ll_modifypass.setOnClickListener(this);
		ll_about = (LinearLayout) view.findViewById(R.id.ll_about);
		ll_about.setOnClickListener(this);
		ll_notice = (LinearLayout) view.findViewById(R.id.ll_notice);
		ll_notice.setOnClickListener(this);
		ll_update = (LinearLayout) view.findViewById(R.id.ll_update);
		ll_update.setOnClickListener(this);
		ll_user = (LinearLayout) view.findViewById(R.id.ll_user);
		ll_user.setOnClickListener(this);
		logOut = (Button) view.findViewById(R.id.logOut);
		logOut.setOnClickListener(this);
		ll_delete = (LinearLayout) view.findViewById(R.id.ll_delete);
		ll_delete.setOnClickListener(this);
		ll_investigate = (LinearLayout) view.findViewById(R.id.ll_investigate);
		ll_investigate.setOnClickListener(this);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		MainActivity.imageview.setVisibility(View.GONE);
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.ll_modifypass:
			if (!((SocoolApplication) SettingFragment.this.getActivity().getApplication()).isOffLineLogin()) {
				startActivity(new Intent().setClass(SettingFragment.this.getActivity(), ModifyPassActivity.class));
			} else {
				mTiPsDialog.show();
			}
			break;
		case R.id.ll_about:
			if (!((SocoolApplication) SettingFragment.this.getActivity().getApplication()).isOffLineLogin()) {
				startActivity(new Intent().setClass(SettingFragment.this.getActivity(), AboutActivity.class));
			} else {
				mTiPsDialog.show();
			}
			break;
		case R.id.ll_notice:
			startActivity(new Intent().setClass(SettingFragment.this.getActivity(), NoticeActivity.class));
			break;
		case R.id.ll_update:
			startActivity(new Intent().setClass(SettingFragment.this.getActivity(), UpdateActivity.class));
			break;
		case R.id.ll_user:
			startActivity(new Intent().setClass(SettingFragment.this.getActivity(), UserActivity.class));
			break;
		case R.id.logOut:
			SinofaithUtil.userInfo = new AppUserInfo();
			startActivity(new Intent().setClass(SettingFragment.this.getActivity(), LoginActivity.class));
			SettingFragment.this.getActivity().finish();
			break;
		case R.id.ll_delete:
			new AlertDialog.Builder(getActivity())
					.setItems(new String[] { "清理3天前数据", "清理5天前数据", "清理1周前数据" }, new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							int day = 0;
							switch (which) {
							case 0:
								day = -3;
								break;
							case 1:
								day = -5;
								break;
							case 2:
								day = -7;
								break;
							}

							final Callback callback = new Callback() {
								@Override
								public boolean handleMessage(Message msg) {
									switch (msg.what) {
									case State.SUCCESS:
										Toast.makeText(SettingFragment.this.getActivity(), "清除缓存成功", Toast.LENGTH_LONG)
												.show();
										break;
									case State.FAIL:
										Toast.makeText(SettingFragment.this.getActivity(), "清除缓存失败", Toast.LENGTH_LONG)
												.show();
										break;
									}
									return false;
								}
							};
							final Handler handler = new Handler(callback);
							final int days = day;
							MyDialog.Builder builder = new MyDialog.Builder(SettingFragment.this.getActivity());
							builder.setTitle("温馨提示").setMessage("此操作会删除对应所有下载的任务和答案，确定要删除吗？")
									.setPositiveButton("确定", new android.content.DialogInterface.OnClickListener() {
										@Override
										public void onClick(DialogInterface dialog, int which) {
											S.deleteTemp(getActivity(), days, handler);
											dialog.dismiss();
										}
									}).setNegativeButton("关闭", new android.content.DialogInterface.OnClickListener() {

										@Override
										public void onClick(DialogInterface dialog, int which) {
											dialog.dismiss();
										}
									}).create().show();
						}
					}).show();

			// AndroidSQLiteOpenHelper dbHelper = new
			// AndroidSQLiteOpenHelper(SettingFragment.this.getActivity());
			// SQLiteDatabase db = dbHelper.getWritableDatabase();
			// db.execSQL("delete from Sif_Task");
			// db.execSQL("delete from Sif_TaskAddress");
			// db.execSQL("delete from Sif_SubTask");
			// db.execSQL("delete from Sif_SubTemplate");
			// db.execSQL("delete from Sif_TaskQuestion");
			// db.execSQL("delete from Sif_QuestionOptions");
			// db.execSQL("delete from QuestionAnswer");
			// db.deleteDatabase(new
			// File(SettingFragment.this.getActivity().deleteDatabase(name)))
			// db.close();

			break;
		case R.id.ll_investigate:
			Intent intent = new Intent(getActivity(), InvestigatesActivity.class);
			getActivity().startActivity(intent);
		default:
			break;
		}
	}

}
